package com.hhw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.Books;
import com.lj.dao.BaseDao;
import com.lj.entity.BorrowBooks;





public class BooksDaolmpl {
	/*
	 * 查询全部书籍的方法
	 */
	public List<Books> findAll(){
		try{
			List<Books> list=new ArrayList<Books>();
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("select * from books");
			ResultSet rs=pstm.executeQuery();
			Books p=null;
			while(rs.next()){
				p=new Books();
				p.setBook_id(rs.getInt(1));
				p.setBook_name(rs.getString(2));
				p.setWriter(rs.getString(3));
				p.setPress(rs.getString(4));
				p.setClassification(rs.getString(5));
				p.setPrice(rs.getDouble(6));
				p.setIsbn(rs.getString(7));
				p.setTotal(rs.getInt(8));
				p.setBorrow_count(rs.getString(9));
				list.add(p);
				System.out.println(p);
			}
			
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
//	/*
//	 * 添加商品方法
//	 */
//	public boolean addBooks(Books p){
//		try{
//			Connection con=BaseDao.getConnection();
//			PreparedStatement pstm=con.prepareStatement("insert into books(Book_id,Book_name,Writer,Press,Classification,Price,Isbn,Total,Borrow_count) values(?,?,?,?,?,?,?,?,?)");
//			pstm.setInt(1, p.getBook_id());
//			pstm.setString(2, p.getBook_name());
//			pstm.setString(3, p.getWriter());
//			pstm.setString(4, p.getPress());
//			pstm.setString(5, p.getClassification());
//			pstm.setDouble(6, p.getPrice());
//			pstm.setString(7, p.getIsbn());
//			pstm.setInt(8, p.getTotal());
//			pstm.setString(9, p.getBorrow_count());
//			int rs = pstm.executeUpdate();
//			if(rs>0){
//				return true;
//			}else{
//				return false;
//			}
//		}catch(Exception e){
//			e.printStackTrace();
//			return false;
//		}
//	}
//	
//	/*
//	 * 验证该商品是否存在<根据id>
//	 */
//	public boolean isExist(int id){
//		return findBooksById(id)==null?false:true;
//	}
//	/*
//	 * 验证该商品是否存在<根据name>
//	 */
//	private Object findBooksById(int id) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	public boolean isExist(String name){
//		return findBooksByName(name)==null?false:true;
//	}
//	
//	try{
//		Connection con=BaseDao.getConnection();
//		PreparedStatement pstm=con.prepareStatement("select * from library where Book_id=?");
//		pstm.setInt(1, );
//		ResultSet rs=pstm.executeQuery();
//		Books p=null;
//		while(rs.next()){
//			p=new Books();
//			p.setBook_id(rs.getInt(1));
//			p.setBook_name(rs.getString(2));
//			p.setWriter(rs.getString(3));
//			p.setPress(rs.getString(4));
//			p.setClassification(rs.getString(5));
//			p.setPrice(rs.getDouble(6));
//			p.setIsbn(rs.getString(7));
//			p.setTotal(rs.getInt(8));
//			p.setBorrow_count(rs.getString(9));
//		}
//		return p;
//	}catch(Exception e){
//		e.printStackTrace();
//		return null;
//	}
	
	//添加书目的方法
	public int addBooks(Books books){
		Connection conn = null;
		PreparedStatement ps = null;
		int pstm = 0;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("insert into books(Book_id,Book_name,Writer,Press,Classification,Price,Isbn,Total,Borrow_count) values(?,?,?,?,?,?,?,?,?)");
			ps.setInt(1, books.getBook_id());
			ps.setString(2,books.getBook_name());
			ps.setString(3,books.getWriter());
			ps.setString(4,books.getPress());
			ps.setString(5,books.getClassification());
			ps.setDouble(6,books.getPrice());
			ps.setString(7,books.getIsbn());
			ps.setInt(8,books.getTotal());
			ps.setString(9,books.getBorrow_count());
			pstm = ps.executeUpdate();
			return pstm;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	
	
	
	
}
	
	
	

